<template>
  <div class="tag-personal-list">
    <tag-content-header search refresh setting @toolFun="toolFun" :settingData="columns">
      <template #title>
        <span>我申请的API列表</span>
      </template>
    </tag-content-header>
    <tag-table
      ref="table"
      @refresh="getList"
      :data="dataList"
      :loading="loading"
      :columns="columns"
      :params="params"
      :total="totalSize"
      @rowClick="rowClick"
    >
      <template #dataPlfm="{ scope }">
        {{ transferPlfm(scope.row.dataPlfm) }}
      </template>
      <template #operation="{ scope }">
        <div class="operation-button-box">
          <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
            <icon-svg name="iconDelete" class="svg-delete" @click.stop.native="handleDelete(scope.row)"></icon-svg>
          </el-tooltip>
        </div>
      </template>
    </tag-table>
    <pac-drawer ref="pacDrawer"></pac-drawer>
  </div>
</template>

<script>
import pacDrawer from './personal/package-personal-drawer'
export default {
  components: {
    pacDrawer
  },
  data() {
    return {
      dataList: [],
      loading: false,
      columns: [
        {
          show: true,
          label: 'API名称',
          prop: 'apiNm',
          search: true,
          searchKey: ''
        },
        {
          show: true,
          label: '应用平台名称',
          prop: 'appPlfmNm',
          search: true,
          searchKey: '',
          align: 'center'
        },
        {
          show: true,
          label: '数据平台名称',
          prop: 'dataPlfm',
          search: true,
          searchKey: '',
          align: 'center'
        },
        {
          show: true,
          label: 'API说明',
          prop: 'apiDesc',
          align: 'center'
        },
        {
          show: true,
          label: '累计调用',
          prop: 'apiCount',
          align: 'center'
        },
        {
          show: true,
          label: '创建时间',
          prop: 'createTm',
          align: 'center'
        },
        {
          show: 'fixed',
          label: '操作',
          prop: 'operation',
          align: 'center',
          width: 200
        }
      ],
      params: {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {},
        descPriority: false
      },
      totalSize: 0
    }
  },
  methods: {
    rowClick(row) {
      if (row.apiId) this.handleView(row)
    },
    getList() {
      this.loading = true
      this.$ajax({
        url: this.$ajax.setUrl('pac/pacAppApi/page'),
        method: 'get',
        params: this.$ajax.setParams(this.params)
      }).then(({ data }) => {
        if (data && data.code === '0') {
          if (data.data.currentPageNum === this.params.currentPageNum) {
            this.dataList = data.data.records
            this.totalSize = data.data.totalSize
          }
        } else {
          this.$message.error(data.data || data.msg)
        }
        this.loading = false
      })
    },
    toolFun(operation) {
      switch (operation) {
        case 'search':
          // 搜索动作
          this.$refs['table'].searchToggle()
          break
        case 'add':
          // 新增动作
          // this.handleEdit()
          break
        case 'refresh':
          // 刷新动作
          this.getList()
          break
      }
    },
    handleView(scope) {
      this.$refs.pacDrawer.showDrawer({
        appId: scope.appId,
        apiId: scope.apiId
      })
    },
    handleDelete(scope) {
      this.$confirm(`确定删除【${scope.apiNm}】？`).then(() => {
        let messageBox = document.getElementsByClassName('el-message-box__wrapper')
        messageBox[0].focus() // 防止按空格键重复确定
        this.$ajax({
          url: this.$ajax.setUrl(`pac/pacAppApi/${scope.appId}/${scope.apiId}`),
          method: 'delete',
          params: this.$ajax.setParams()
        }).then(({ data }) => {
          if (data && data.code === '0') {
            this.$message.success(data.msg)
            this.getList()
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    transferPlfm(plfm) {
      let plfmNm = []
      plfm.forEach(item => plfmNm.push(item.plfmNm))
      return plfmNm.join(',')
    }
  }
}
</script>

<style lang="scss">
.tag-personal-list {
  .tag-table .el-table__row {
    cursor: pointer;
  }
}
</style>
